package controller;

import java.sql.SQLException;
import java.util.ArrayList;

import db.DBConnection;
import db.DBEmployee;
import db.DBEmployeeJob;
import db.DBPerson;
import db.IFEmployee;
import db.IFEmployeeJob;
import db.IFPerson;
import model.Employee;
import model.EmployeeJob;

public class EmployeeController
{
	private static EmployeeController instance = null;

	public static EmployeeController getInstance()
	{
		if(instance == null)
			instance = new EmployeeController();

		return instance;
	}

	public EmployeeController()
	{

	}

	public int createEmployee(String fName, String lName, String email, String phoneNo, String username, String password, boolean active, EmployeeJob job)
	{
		int result = -3;

		try 
		{		
			Employee newEmployee = new Employee();

			newEmployee.setfName(fName);
			newEmployee.setlName(lName);
			newEmployee.setEmail(email);
			newEmployee.setPhoneNo(phoneNo);
			newEmployee.setUsername(username);
			newEmployee.setPassword(password);
			newEmployee.setActive(active);
			newEmployee.setJob(job);

			DBConnection.startTransaction();

			IFEmployee dbEmployee = new DBEmployee();
			result = dbEmployee.createEmployee(newEmployee);

			DBConnection.commitTransaction();
		}
		catch (Exception e)
		{
			DBConnection.rollbackTransaction();
		}

		return result;
	}

	public int updateEmployee(int employeeID, String fName, String lName, String email, String phoneNo, String username, String password, boolean active, EmployeeJob job)
	{
		int result = -3;

		try 
		{		
			Employee newEmployee = new Employee();

			newEmployee.setPersonID(employeeID);
			newEmployee.setfName(fName);
			newEmployee.setlName(lName);
			newEmployee.setEmail(email);
			newEmployee.setPhoneNo(phoneNo);
			newEmployee.setUsername(username);
			newEmployee.setPassword(password);
			newEmployee.setActive(active);
			newEmployee.setJob(job);

			DBConnection.startTransaction();

			IFEmployee dbEmployee = new DBEmployee();
			result = dbEmployee.updateEmployee(newEmployee);

			DBConnection.commitTransaction();
		}
		catch (Exception e)
		{
			DBConnection.rollbackTransaction();
		}

		return result;
	}

	public int activateEmployee(int employeeID) throws SQLException
	{
		IFPerson dbPerson = new DBPerson();

		return dbPerson.activatePerson(employeeID);
	}

	public Employee searchEmployeeID(int id) throws SQLException
	{
		IFEmployee dbEmployee = new DBEmployee();

		return dbEmployee.searchEmployeeID(id);
	}

	public ArrayList<Employee> searchEmployeeUsername(boolean active, String jobName, String search) throws SQLException
	{
		IFEmployee dbEmployee = new DBEmployee();

		return dbEmployee.searchEmployeeUsername(active, jobName, search);
	}

	public ArrayList<Employee> searchEmployeePhoneNo(boolean active, String jobName, String search) throws SQLException
	{
		IFEmployee dbEmployee = new DBEmployee();

		return dbEmployee.searchEmployeePhoneNo(active, jobName, search);
	}

	public ArrayList<Employee> searchEmployeeEmail(boolean active, String jobName, String search) throws SQLException
	{
		IFEmployee dbEmployee = new DBEmployee();

		return dbEmployee.searchEmployeeEmail(active, jobName, search);
	}

	public ArrayList<Employee> searchEmployeeEverything(boolean active, String jobName, String search) throws SQLException
	{
		IFEmployee dbEmployee = new DBEmployee();

		return dbEmployee.searchEmployeeEverything(active, jobName, search);
	}

	public ArrayList<Employee> searchEmployeeName(boolean active, String jobName, String search) throws SQLException
	{
		IFEmployee dbEmployee = new DBEmployee();

		return dbEmployee.searchEmployeeName(active, jobName, search);
	}

	public int checkLogin(String loginUsername, char[] loginPassword) throws SQLException
	{
		IFEmployee dbEmployee = new DBEmployee();

		return dbEmployee.checkLogin(loginUsername, loginPassword);
	}

	/*
	 * 
	 * Employee Jobs
	 * 
	 */
	public int createEmployeeJob(String jobName)
	{
		int result = -3;

		try 
		{		
			EmployeeJob newEmployeeJob = new EmployeeJob();

			newEmployeeJob.setJobName(jobName);

			DBConnection.startTransaction();

			IFEmployeeJob dbEmployeeJob = new DBEmployeeJob();
			result = dbEmployeeJob.createEmployeeJob(newEmployeeJob);

			DBConnection.commitTransaction();
		}
		catch (Exception e)
		{
			DBConnection.rollbackTransaction();
		}

		return result;
	}

	public int updateEmployeeJob(int jobID, String jobName)
	{
		int result = -3;

		try 
		{		
			EmployeeJob newEmployeeJob = new EmployeeJob();

			newEmployeeJob.setJobID(jobID);
			newEmployeeJob.setJobName(jobName);

			DBConnection.startTransaction();

			IFEmployeeJob dbEmployeeJob = new DBEmployeeJob();
			result = dbEmployeeJob.updateEmployeeJob(newEmployeeJob);

			DBConnection.commitTransaction();
		}
		catch (Exception e)
		{
			DBConnection.rollbackTransaction();
		}

		return result;
	}

	public ArrayList<EmployeeJob> printAllEmployeeJobs() throws SQLException
	{
		IFEmployeeJob dbEmployeeJob = new DBEmployeeJob();

		return dbEmployeeJob.printAllEmployeeJobs();
	}

	public EmployeeJob findEmployeeJobByName(String jobName) throws SQLException
	{
		IFEmployeeJob dbEmployeeJob = new DBEmployeeJob();

		return dbEmployeeJob.findEmployeeJobByName(jobName);
	}
}